package com.explorx.sns.puma.im.core.chat.impl;

import com.explorex.puma.common.utils.SnowflakeIdWorker;
import com.explorx.sns.puma.im.core.chat.UserInfoDomainService;
import com.explorx.sns.puma.im.core.chat.consts.UserStatusEnum;
import com.explorx.sns.puma.im.core.chat.model.RegisterModel;
import com.explorx.sns.puma.im.core.chat.model.UserInfoModel;
import com.explorx.sns.puma.im.core.dal.dao.UserInfoEntityMapper;
import com.explorx.sns.puma.im.core.dal.entity.UserInfoEntity;
import com.explorx.sns.puma.im.core.dal.entity.UserInfoEntityExample;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class UserInfoDomainServiceImpl implements UserInfoDomainService {

    private String DEDAULT_AVATAR="https://moment-1256541791.cos.ap-shanghai.myqcloud.com/config/year-celebration-clock-accessories.jpg";

    @Resource
    private UserInfoEntityMapper userInfoMapper;


    @Override
    public List<UserInfoModel> getUsersInfo(List<Long> uids) {
        final UserInfoEntityExample userInfoExample = new UserInfoEntityExample();
        userInfoExample.createCriteria().andUserIdIn(uids);
        final List<UserInfoEntity> userInfos = userInfoMapper.selectByExample(userInfoExample);
        if (CollectionUtils.isEmpty(userInfos)) {
            return Collections.emptyList();
        }
        return userInfos.stream().map(ele -> transfer(ele)).collect(Collectors.toList());
    }

    private UserInfoModel transfer(UserInfoEntity entity) {
        return UserInfoModel.builder().userId(entity.getUserId())
                .userName(entity.getUserName())
                .avatar(entity.getAvatar())
                .age(entity.getAge())
                .birthDay(Optional.ofNullable(entity.getBirthday()).orElse("未知"))
                .city(Optional.ofNullable(entity.getCity()).orElse(0).toString()) // todo
                .gender(entity.getGender())
                .school(Optional.ofNullable(entity.getSchool()).orElse("未知"))
                .email(entity.getEmail())
                .build();
    }

    @Override
    public UserInfoModel getUserInfo(Long curUid) {
        final UserInfoEntityExample userInfoExample = new UserInfoEntityExample();
        userInfoExample.createCriteria()
                .andUserIdEqualTo(curUid)
                .andStatusEqualTo((byte) UserStatusEnum.OK.getValue());
        final List<UserInfoEntity> userInfos = userInfoMapper.selectByExample(userInfoExample);
        if (CollectionUtils.isEmpty(userInfos)) {
            return null;
        }
        return transfer(userInfos.get(0));
    }

    @Override
    public UserInfoModel userLogin(String phone, String pwd) {
        final UserInfoEntityExample userInfoExample = new UserInfoEntityExample();
        userInfoExample.createCriteria()
                .andPasswordEqualTo(pwd)
                .andPhoneEqualTo(phone)
                .andStatusEqualTo((byte) UserStatusEnum.OK.getValue());
        final List<UserInfoEntity> userInfos = userInfoMapper.selectByExample(userInfoExample);
        if (CollectionUtils.isEmpty(userInfos)) {
            return null;
        }
        return transfer(userInfos.get(0));
    }

    @Override
    public Long register(RegisterModel registerModel) {
        final long userId = SnowflakeIdWorker.nextId();
        UserInfoEntity userInfoEntity = new UserInfoEntity();
        userInfoEntity.setAvatar(DEDAULT_AVATAR);
        userInfoEntity.setUserName(registerModel.getUsername());
        userInfoEntity.setPassword(registerModel.getPassword());
        userInfoEntity.setUserId(userId);
        userInfoEntity.setPhone(registerModel.getPhoneNum());
        if (userInfoMapper.insertSelective(userInfoEntity) > 0) {
            return userId;
        }else {
            return 0L;
        }
    }

    @Override
    public void editName(Long uid, String name) {
        UserInfoEntity entity=new UserInfoEntity();
        entity.setUserName(name);
        UserInfoEntityExample example=new UserInfoEntityExample();
        example.createCriteria().andUserIdEqualTo(uid);
        userInfoMapper.updateByExampleSelective(entity, example);
    }

    @Override
    public void editBirthDay(Long uid, String birthDay) {
        UserInfoEntity entity=new UserInfoEntity();
        entity.setBirthday(birthDay);
        UserInfoEntityExample example=new UserInfoEntityExample();
        example.createCriteria().andUserIdEqualTo(uid);
        userInfoMapper.updateByExampleSelective(entity, example);
    }

    @Override
    public void editGender(Long uid, int gender) {
        UserInfoEntity entity=new UserInfoEntity();
        entity.setGender((byte) gender);
        UserInfoEntityExample example=new UserInfoEntityExample();
        example.createCriteria().andUserIdEqualTo(uid);
        userInfoMapper.updateByExampleSelective(entity, example);
    }

    @Override
    public void editEducation(Long uid, String school) {
        UserInfoEntity entity=new UserInfoEntity();
        entity.setSchool(school);
        UserInfoEntityExample example=new UserInfoEntityExample();
        example.createCriteria().andUserIdEqualTo(uid);
        userInfoMapper.updateByExampleSelective(entity, example);
    }

    @Override
    public void editAge(Long uid, int age) {
        UserInfoEntity entity=new UserInfoEntity();
        entity.setAge(age);
        UserInfoEntityExample example=new UserInfoEntityExample();
        example.createCriteria().andUserIdEqualTo(uid);
        userInfoMapper.updateByExampleSelective(entity, example);
    }

    @Override
    public void editCity(Long uid, int cityCode) {
        UserInfoEntity entity=new UserInfoEntity();
        entity.setCity(cityCode);
        UserInfoEntityExample example=new UserInfoEntityExample();
        example.createCriteria().andUserIdEqualTo(uid);
        userInfoMapper.updateByExampleSelective(entity, example);
    }

    @Override
    public void editAvatar(Long uid, String avatar) {
        UserInfoEntity entity=new UserInfoEntity();
        entity.setAvatar(avatar);
        UserInfoEntityExample example=new UserInfoEntityExample();
        example.createCriteria().andUserIdEqualTo(uid);
        userInfoMapper.updateByExampleSelective(entity, example);
    }
}
